<template>
  <div>
    <!-- 顶部搜搜索框 -->
    <div>
      <div class="top_conntainer">
        <router-link to="/">
          <img class="back" src="../assets/back.png" alt="" />
        </router-link>
        <input
          :value="str"
          @input="doit"
          @keyup.13="search"
          id="search"
          type="text"
          placeholder="搜索美食"
        />
        <img
          @click="search"
          class="search_btn"
          src="../assets/search.png"
          alt=""
        />
      </div>
    </div>
    <div v-if="history" class="w-90">
      <div class="history">
        <!-- 历史搜索 -->
        <p>历史搜索</p>
        <img class="del" src="../assets/del.png" alt="" />
        <span class="history_item">红烧肉</span>
      </div>
      <div class="want">
        <!-- 猜你想搜 -->
        <p>猜你想搜</p>
        <span class="history_item">面食</span>
        <span class="history_item">冬瓜</span>
        <span class="history_item">玉米</span>
        <span class="history_item">红烧肉</span>
      </div>
    </div>
    <div v-else class="w-90">
      <div v-if="search_name.length==0" class="null">
        <img class="null_img" src="../assets/notmore.png" alt=""><br>
        没有搜索到相关内容
      </div>
      <div class="hot" v-else>
          <div class="hot_item" v-for="(v, k) of search_name" :key="k">
        <router-link :to="`/detail/${v.id}`">

            <img class="hot_img" :src="v.image" alt="" /><br />
            {{ v.subject }}{{v.id}}
        </router-link>

          </div>
      </div>
    </div>
  </div>
</template>
<style>
.mint-button::after {
  border: 0;
  background-color: #fff;
}
</style>
<style>
.null{
  font-size: 20px;
  text-align: center;
  margin-top: 100px;
}
.null_img{
  width: 100px;
  
}
.top_container {
  position: relative;
}
#search {
  width: 80%;
  height: 30px;
  background-color: #e8e8e8;
  border: 0;
  border-radius: 15px;
  padding-left: 15px;
  margin-top: 20px;
}
.search_btn {
  position: absolute;
  right: 25px;
  top: 25px;
}
.back,
.search_btn {
  width: 20px;
  margin: 0 10px;
}
.history,
.want {
  margin-top: 30px;
  font-size: 14px;
  position: relative;
}
.del {
  width: 20px;
  position: absolute;
  right: 0;
  top: 0;
}
.history_item {
  padding: 7px 10px;
  background-color: #eee;
  display: inline-block;
  margin: 10px;
  border-radius: 11px;
  font-size: 12px;
}
</style>
<script>
export default {
  data() {
    return {
      str: "",
      search_name: [],
      history: true,
    };
  },
  methods: {
    search() {
      this.history = false;
      this.str = "%" + this.str + "%";
      if (this.str != "%%") {
        this.axios
          .get("/search_name", {
            params: {
              search: this.str,
            },
          })
          .then((res) => {
            this.search_name = [];
            let result = res.data.result;
            console.log(result);
            result.forEach((item) => {
              if (item.image != null) {
                item.image = require("../assets/articleimages/" + item.image);
              }
              this.search_name.push(item);
            });
            console.log(this.search_name);
            if (this.search_name.length==0) {
              console.log("没有搜索到相关内容");
            }
          });
      } else {
        console.log("搜索内容不能为空");
      }
      this.str = "";
      // let textarea=document.getElementById('search')
      // textarea.value=""
    },
    doit(e) {
      this.str = e.target.value;
      // console.log(this.str)
    },
  },
};
</script>